package com.android.kaui.ext;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public class KauiView  extends View {
	public static final String TAG = "KauiView";
	
	private static final float TOUCH_TOLERANCE = 4;
	
	private Paint mPenPaint;
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    
	private float mX, mY;
	
	public KauiView(Context context) {
		super(context);
		Log.d(TAG,"KauiView Construct <context>");
		
		initKauiView();
	}
	
	public KauiView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		Log.d(TAG,"KauiView Construct <context, attrs>");
		
		initKauiView();
	}
	
	public void initKauiView()
	{
		Log.d(TAG,"initKauiView...................");
		
		mPenPaint = new Paint();
		mPenPaint.setAntiAlias(true);
		mPenPaint.setDither(true);
		mPenPaint.setColor(0x33FFFFFF);
	    mPenPaint.setStyle(Paint.Style.STROKE);
	    mPenPaint.setStrokeJoin(Paint.Join.ROUND);
	    mPenPaint.setStrokeCap(Paint.Cap.ROUND);
	    mPenPaint.setStrokeWidth(8);
	    mPenPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
	    
	    mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
        mPath = new Path();
        mCanvas = new Canvas(mBitmap);
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		drawPen(canvas);
	}
	
	
	private void drawPen(Canvas _canvas)
	{
//		Log.d(TAG, "drawPen.......");
		
		_canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
		_canvas.drawPath(mPath, mPenPaint);
	}
	
	private void touch_start(float _x, float _y) {
        mPath.reset();
        mPath.moveTo(_x, _y);
        mX = _x;
        mY = _y;
    }
	
	
	private void touch_move(float _x, float _y) {
        float dx = Math.abs(_x - mX);
        float dy = Math.abs(_y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (_x + mX)/2, (_y + mY)/2);
            mX = _x;
            mY = _y;
        }
    }
    
    
	private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPenPaint);
        // kill this so we don't double draw
        mPath.reset();
    }
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		float x = event.getX();
        float y = event.getY() - 170;
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            	touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
            	touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            	touch_up();
                invalidate();
                break;
        }
		return false;
	}
}